package com.c.ctgproject.ui.collect

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.c.ctgproject.adapter.guang.BatteryInWarehouseAdapter
import com.c.ctgproject.base.BaseActivityV2
import com.c.ctgproject.databinding.ActivitySearchBatteryBinding
import com.c.ctgproject.entity.guang.BatteryInWarehouseEntity
import com.c.ctgproject.http.ApiMethods
import com.c.ctgproject.http.MyObserverHandlerLoading
import com.c.ctgproject.ui.files.MainActivity
import com.c.ctgproject.utils.*
import org.jetbrains.anko.startActivity

class SearchBatteryActivity : BaseActivityV2() {
    private val mBinding by lazy { ActivitySearchBatteryBinding.inflate(layoutInflater) }
    override fun getLayout()=mBinding.root
    private var page = 1
    private var search: String = ""
    private var mList = ArrayList<BatteryInWarehouseEntity.DataBean.ListBean>()
    private val mAdapter by lazy { BatteryInWarehouseAdapter(this, mList, true, type = 2,null) }
    override fun event() {
        search=intent.getStringExtra(MyConstants.SEARCH)
        mBinding.mSearchET.setText(search)
        getData()
    }

    override fun click() {
        mBinding.mCarRV.adapter = mAdapter
        mBinding.mCarRV.layoutManager = LinearLayoutManager(this)


        mBinding.mSearchET.handlerText {
            if (it.isEmpty() || it == "") {
                search = ""
                page = 1
                getData()
            }
        }
        mBinding.mSearchTV.setOnClickListener {
            search = mBinding.mSearchET.text.handlerNull()
            page = 1
            getData()
        }
        mBinding.mRefresh.setOnLoadMoreListener {
            page++
            getData()
        }

        mBinding.mRefresh.setOnRefreshListener {
            page = 1
            getData()
        }
    }
    fun getData() {
        //查询类型 2-待过磅 3-待入库 1.默认我的录入
        ApiMethods.apiSubscribe(ApiMethods.retrofit.powerBatteryList(search, page), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
            MyLog.d("我操作过的电池:$it")
            if (page == 1) mList.clear()
            val data = getGson().fromJson(it, BatteryInWarehouseEntity::class.java).data
            data?.list?.let { list ->
                mList.addAll(list)
                mAdapter.notifyDataSetChanged()
                mBinding.mCarRV.handlerPosition(page)
            }
        })
    }
}